package com.fwx.chatroom.testWeb;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
public class TestWebSocketController extends TextWebSocketHandler {
    /*在websocket建立连接后自动调用*/
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("连接建立成功！");
    }
    /*websocket收到消息进行调用*/
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        System.out.println("handleTextMessage收到消息！！！" + message.toString());
        //session记录了通信双方是谁
        session.sendMessage(message);
    }
    /*连接出现异常自动调用*/
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("handleTransportError收到消息");
    }
    //连接正常关闭调用的方法
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("afterConnectionClosed收到消息,连接断开");
    }


}
